import Vue from 'vue'
import VueRouter from 'vue-router'
import login from './components/login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import User from './components/user/User.vue'
import Rights from './components/power/Rights.vue'
import Roles from './components/power/Roles.vue'
import Categories from './components/Cate/Categories.vue'
import Params from './components/Cate/Params.vue'
import GoodsList from './components/Cate/goods_list.vue'
import Add from './components/Cate/addGoods.vue'
import Report from './components/report/Report.vue'
Vue.use(VueRouter)

const router = new VueRouter({

  routes: [
    {
      path: '/', redirect: '/login'
    },
    {
      path: '/login', component: login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: GoodsList },
        { path: '/goods/add', component: Add },
        { path: '/reports', component: Report }
      ]
    }
  ]
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // next()给参就数要跳转的路径
  if (!window.sessionStorage.getItem('token')) return next('/login')
  // 放行函数
  next()
})

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
